package org.hackystat.ui.sensordataviewer.server;

import java.util.ArrayList;

import javax.xml.datatype.XMLGregorianCalendar;

/**
 * This class represents one event of sensor data elements in the UI display, an event is
 * quantized to the the sensor data types occurring within one minute that have the same 
 * type and tool attributes.
 * @author David J. Nickles
 */
public class Event {
  
  private String root;
  private ArrayList<SensorDataItem> collection;
  private XMLGregorianCalendar timestamp;
  private String xmlTimestamp;
  private long id;
  private String type;
  private String tool;
  private int count;
  
  /**
   * Makes a new SDVEvent containing this item.
   * @param item a sensor data element
   */
  public Event(SensorDataItem item) {
    this.count = 0;
    this.collection = new ArrayList<SensorDataItem>();
    add(item);
  }
  

  /**
   * This method adds the sensor data element to this event.
   * @param item a sensor data element to be added to this event.
   * @return true if this set did not already contain the specified element.
   */
  public final boolean add(SensorDataItem item) {
    if (this.collection.isEmpty()) {
      this.timestamp = item.getEventTimestamp();
      this.id = this.timestamp.toGregorianCalendar().getTimeInMillis();
      this.xmlTimestamp = item.formatEventTimestamp();
      this.type = item.getType();
      this.tool = item.getTool();
    }
    boolean isNewAddition = this.collection.add(item);
    if (isNewAddition) {
      this.count++;
    }
    return isNewAddition;
  }
  
  /**
   * Gets the count of new elements.
   * @return the count of new elements.
   */
  public int size() {
    return this.count;
  }
  
  /**
   * Gets a TreeItem populated with the SDVItems within this SDVEvent.
   * @return TreeItem containing with the SDVItems within this SDVEvent
   */
  public String toString() {
    this.root = this.xmlTimestamp + " " + this.type + " " + this.tool;
    // + "[" + this.collection.size() + "]";
    return this.root;
  }
  
  /**
   * Gets a TreeItem populated with the SDBItems within this SDVEvent.
   * @return TreeItem containing with the SDBItems within this SDVEvent
   */
  public ArrayList<String> getChildren() {
    ArrayList<String> children = new ArrayList<String>();
    for (SensorDataItem i : this.collection) {
      children.add(i.getHref());
    }
    return children;
  }
  
  /**
   * Gets the value of the timestamp property.
   * @return Date
   */
  public long getID() {
    return this.id;
  }
  
  /**
   * Gets the value of the sensorDataType property.
   * @return String
   */
  public String getSensorDataType() {
    return this.type;
  }
  
  /**
   * Gets the value of the tool property.
   * @return String
   */
  public String getTool() {
    return this.tool;
  }
  
  /**
   * This method tests if the sensor data element's attributes classify it within this event.
   * @param item the sensor data element
   * @return true if it occurs in this event, false if it does not
   */
  public boolean acceptsThis(SensorDataItem item) {
    boolean result = false;
    if (this.timestamp.equals(item.getEventTimestamp())
        && this.type.equals(item.getType())
        && this.tool.equals(item.getTool())) {
      result = true;
    }
    return result;
  }
}